﻿Ext.define('Cps.view.dashboard.popup.Search2', {
	extend: 'Ext.window.Window',
	alias: 'widget.Search2',
	
	height: 500,
	width: 800,
	title: Cps.Locale.Search.title,
	modal: true,
	closable: true,
	resizable: false,
	layout: {type: 'vbox', align: 'stretch'},
	items: [
		{
			xtype: 'form',
			bodyPadding: '7px 20px',
			layout: 'hbox',
			items: [
				{
					xtype: 'fieldcontainer',
					layout: 'vbox',
					width: 470,
					//flex: 3,
					items: [
						{
							xtype: 'textfield',
							id: 'txtsearchTag',
							labelWidth: 70,
							width: 466,
							fieldLabel: Cps.Locale.Search.field,
							listeners: {
				        specialkey: function(field, e){
				          if (e.getKey() == e.ENTER) {
										loadData1();
									}
								}
							}
						}, 
						{
							xtype: 'checkboxfield',
							id: 'chkRegex',
							margin: '0 0 0 75',
							labelSeparator: '',
							checked: true,
							hideLabel: true,
							boxLabel: Cps.Locale.Search.field1,
							fieldLabel: Cps.Locale.Search.field1,
							listeners: {
								change: function () {
									loadData1();
								}
							}
						}
					]
				},
				{
					xtype: 'button',
					width: 120,
					//flex: 1,
					text: Cps.Locale.Search.btn1,
					handler: function() {
						loadData1();
					}
				}
			],
		},
		{
			xtype: 'grid',
			id: 'gridClient',
			flex: 2,
			store: Ext.create('Ext.data.Store', {
				fields: [
					{ name: 'ClientId', type: 'int' },
					{ name: 'ClientCode', type: 'int' },
					{ name: 'ClientName', type: 'string' }
				],
				data: []
			}),
			columns: {
				items: [
					{
						text: Cps.Locale.Search.column1,
						width: 100,
						hideable: false,
						dataIndex: 'ClientCode',
						renderer: function(data, meta, rec, row, col) {
							if(!data)
								data = '';
							else{
								str = Ext.util.Format.number(data, '0,000');
								return str;
							}
							return data;
						}
					},
					{
						text: Cps.Locale.Search.column2,
						flex: 2,
						hideable: false,
						dataIndex: 'ClientName'
					}
				]				
			},
			listeners: {
				itemdblclick: function( grid, record, item, index, e, eOpts ){
					returnData();
				}
			}
		}
	],
	buttons: [
		{
			text: Cps.Locale.Search.btn2,
			handler: function() {
				returnData();
			}
		},
		{
			text: Cps.Locale.Search.btn3,
			handler: function() {
				this.ownerCt.ownerCt.close();
			}
		}
	],
	listeners:{
    close:function( panel, eOpts ){
    	var grid= panel.down('grid');
			var store= grid.getStore();
			store.removeAll();
    }
  }
});

function loadData1(){
	var gridClient = Ext.getCmp('gridClient');
	Ext.Ajax.request({
		method: 'GET',
		url: Cps.Config.Api.getClient,
		params: function() {
			var value = getParam(), chkRegex = Ext.getCmp('chkRegex'), txtsearchTag = Ext.getCmp('txtsearchTag');
			if(chkRegex.getValue())
				value.isActive = chkRegex.getValue();
			else
				value.isActive = chkRegex.getValue();
			value.searchTag = txtsearchTag.getValue();
			return value;
		},
		success: function(data) {
			if (IsJson(data.responseText))
			{
				data = Ext.JSON.decode(data.responseText).data;
				data = data ? data.ttClient : null;
				data = data ? data.ttClientRow : null;
				var f_data = [];
				if (data != undefined)
				{
					if (data.length != undefined)
					{
						for(i = 0; i < data.length; i++) {
							f_data.push(data[i]);
						}
					}
					else
						f_data.push(data);
				}
				gridClient.store.loadData(f_data);
				gridClient.getView().focusRow(0);
			}
		},
		scope: this
	});
}
function returnData(){
	var txtClientCode = Ext.getCmp('txtClientCode'),
		txtClientName = Ext.getCmp('txtClientName'),
		hdfClientId = Ext.getCmp('hdfClientId'),
		gridClient = Ext.getCmp('gridClient'), 
		select;
	if (gridClient.getSelectionModel().getSelection() != "")
	{
		select = gridClient.getSelectionModel().getSelection();
		var clientcode = select[0].data.ClientCode;
		clientcode = Ext.util.Format.number(clientcode, '0,000');
		clientcode = clientcode.replace(/[,|.]/g,"'");
		txtClientCode.setValue(clientcode);
		txtClientName.setValue(select[0].data.ClientName);
		hdfClientId.setValue(select[0].data.ClientId);
	}
	var win= gridClient.up('window');
	win.close();
}
